package com.navinfo.truckload;

import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author machi
 * @create 2019/01/16
 **/
public class FuelConsumption {
    public static Logger log = LoggerFactory.getLogger(FuelConsumption.class);
    private static String[] readHeader = {"gpsTime", "latitude", "longitude", "height", "engineOutputTorque", "speed", "accelerator", "brake",
            "rotation", "gear", "clutchSwitch", "realTimeOilConsumption", "fuelConsumptionRate"};

    public static void main(String[] args) {
        File dir = new File("C:\\Users\\lenovo\\Desktop\\convertcsv.csv");
        Collection<File> files = new ArrayList<>();
        if (dir.isDirectory()) {
            files = FileUtils.listFiles(dir,
                    new String[]{"csv"}, false);
        } else {
            files.add(dir);
        }

        try {
            for (File file : files) {
                List<CSVRecord> read = CsvUtil.read(file.getAbsolutePath(), readHeader, "gbk");

                //米
                double mileage = 0d;
                //毫升
                double fuel = 0d;
                long preGpstime = 0;
                for (CSVRecord record : read) {
                    String gpsTime = record.get("gpsTime");
                    String fuelConsumptionRate = Optional.ofNullable(record.get("fuelConsumptionRate")).orElse("0");
                    String height = Optional.ofNullable(record.get("height")).orElse("0");
                    String speed = Optional.ofNullable(record.get("speed")).orElse("0");
                    String accelerator = Optional.ofNullable(record.get("accelerator")).orElse("0");

                    //转成秒
                    int duration = 0;
                    if (preGpstime == 0) {
                        duration = 1;
                    } else {
                        duration = (int) ((Long.parseLong(gpsTime) - preGpstime) / 1000);
                    }

                    //换算成m/s
                    BigDecimal spdMsBig = new BigDecimal(speed).
                            divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).
                            divide(BigDecimal.valueOf(3.6), 2, BigDecimal.ROUND_HALF_UP);

                    //换算成ml/s
                    BigDecimal fuelConsumptionRateBig = new BigDecimal(fuelConsumptionRate).
                            divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).
                            divide(BigDecimal.valueOf(3.6), 2, BigDecimal.ROUND_HALF_UP);

                    //发动机燃油消耗率(ml/s)*两点时间差
                    BigDecimal diffFuelBig = fuelConsumptionRateBig.multiply(BigDecimal.valueOf(duration));
                    double dffOil = diffFuelBig.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    //行程内油耗油耗(单位:ml)
                    fuel = fuel + dffOil;

                    //速度(m/s)*两点时间差
                    BigDecimal diffMileageBig = spdMsBig.multiply(BigDecimal.valueOf(duration));
                    double diffMileage = diffMileageBig.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    //里程(单位:m)
                    mileage = mileage + diffMileage;

                }
                double averageFuelConsumption = BigDecimal.valueOf(fuel * 100).
                        divide(BigDecimal.valueOf(mileage), 2, BigDecimal.ROUND_HALF_UP).doubleValue();

                System.out.println(
                        "mileage:" + mileage + ",fuel:" + fuel + ",averageFuelConsumption:" + averageFuelConsumption);
                log.info("mileage:{},fuel:{},averageFuelConsumption:{}", mileage, fuel, averageFuelConsumption);
            }
            //File directory = new File(FuelConsumption.class.getProtectionDomain()
            //        .getCodeSource()
            //        .getLocation()
            //        .toURI()
            //        .getPath()).getParentFile();
            //
            //String s = new DateTime().toString("yyyyMMddHHmmss");
            //String path = directory.getAbsolutePath() + File.separator + s + ".csv";
            //CsvUtil.write(path, writeHeader, result);
        } catch (
                Exception e) {
            log.error("", e);
        }
    }

}
